﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionRoc.Core;
using VisionRoc.Tool;

namespace VisionRoc_Measure
{
    /// <summary>
    /// 椭圆工具待完善
    /// </summary>
    public partial class FormEllipseTool : Form
    {
        HObject m_Image;
        SetEllipse m_SetEllipse;
        public FormEllipseTool(HObject _Image, SetEllipse _m_SetEllipse)
        {
            try
            {
                InitializeComponent();
                HOperatorSet.GenEmptyObj(out m_Image);
                m_Image = _Image;
                m_SetEllipse = _m_SetEllipse;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
           
        }

        private void btn_DrawEllipse_Click(object sender, EventArgs e)
        {
            try
            {
                btn_DrawEllipse.Enabled = false;
                btn_Clear.Enabled = false;
                button2.Enabled = false;
                btn_Refresh.Enabled = false;
                btn_SaveParameter.Enabled = false;
                hWindowControl1.Focus();
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "左键绘制，右键结束", "window", 20, 20, 15, "green");
                HOperatorSet.DrawEllipse(hWindowControl1.HalconWindow, out HTuple Row, out HTuple Column, out HTuple Phi, out HTuple Radius1, out HTuple Radius2);
                HOperatorSet.GenEllipse(out HObject ellipse, Row, Column, Phi, Radius1, Radius2);

                m_SetEllipse.Row = Row.D;
                m_SetEllipse.Col = Column.D;
                m_SetEllipse.Phi = Phi.D;
                m_SetEllipse.Raduise1 = Radius1.D;
                m_SetEllipse.Raduise2 = Radius2.D;
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "绘制成功", "window", 20, 20, 15, "green");
                btn_DrawEllipse.Enabled = true;
                btn_Clear.Enabled = true;
                button2.Enabled = true;
                btn_Refresh.Enabled = true;
                btn_SaveParameter.Enabled = true;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        

        }

        private void FormEllipseTool_Load(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.SetDraw(this.hWindowControl1.HalconWindow, "margin");
                HOperatorSet.SetColor(this.hWindowControl1.HalconWindow, "red");
                HOperatorSet.SetColored(this.hWindowControl1.HalconWindow, 12);
                HOperatorSet.SetLineWidth(this.hWindowControl1.HalconWindow, 2);
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text += "图像为空，请加载图像！";
                }
                else
                {
                    HTuple Width = new HTuple(), Height = new HTuple();
                    HOperatorSet.GetImageSize(m_Image, out Width, out Height);
                    MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Width, Height);
                    m_Image.DispObj(hWindowControl1.HalconWindow);
                    tool_ResultInfo.Text = "图像加载成功。";
                }
                if (m_SetEllipse != null)
                {
                    m_SetEllipse.ReadParameter();
                    Combox_LineWidth.Text = m_SetEllipse.LineWidth.ToString();
                    Combox_CrossSize.Text = m_SetEllipse.CrossSize.ToString();
                    Combox_CrossColor.Text = m_SetEllipse.CrossColor.ToString();
                    Combox_CalibColor.Text = m_SetEllipse.CalibColor.ToString();
                    Combox_LinesColor.Text = m_SetEllipse.EllipseColor.ToString();
                    this.propertyGrid1.SelectedObject = m_SetEllipse;
                    this.propertyGrid1.Refresh();
                    RefreshContour();
                }
                else
                {
                    tool_ResultInfo.Text += "线段工具模块对象不存在。";
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
          
        }


        /// <summary>
        /// 控件刷新
        /// </summary>
        public void RefreshContour()
        {
            if (m_SetEllipse.DisplayLoction == true && m_SetEllipse.DisplayEllipse == true && m_SetEllipse.DisplayCalib == true)
            {
                checkBo_All.Checked = true;
            }
            else
            {
                if (m_SetEllipse.DisplayLoction)
                {
                    checkBox_Location.Checked = true;
                }
                else
                {
                    checkBox_Location.Checked = false;
                    m_SetEllipse.DisplayLoction = false;
                    if (checkBo_All.Checked)
                    {
                        checkBo_All.Checked = false;
                    }
                }

                if (m_SetEllipse.DisplayEllipse)
                {
                    checkBox_Lines.Checked = true;
                }
                else
                {
                    checkBox_Lines.Checked = false;
                    m_SetEllipse.DisplayEllipse = false;
                    if (checkBo_All.Checked)
                    {
                        checkBo_All.Checked = false;
                    }
                }

                if (m_SetEllipse.DisplayCalib)
                {
                    checkBox_Caliper.Checked = true;
                }
                else
                {
                    checkBox_Caliper.Checked = false;
                    m_SetEllipse.DisplayCalib = false;
                    if (checkBo_All.Checked)
                    {
                        checkBo_All.Checked = false;
                    }
                }
            }
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            HOperatorSet.SetColor(this.hWindowControl1.HalconWindow, "red");
            HOperatorSet.SetLineWidth(this.hWindowControl1.HalconWindow, 2);
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            HTuple Width = new HTuple(), Height = new HTuple();
            HOperatorSet.GetImageSize(m_Image, out Width, out Height);
            MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Width, Height);
            m_Image.DispObj(hWindowControl1.HalconWindow);
            m_SetEllipse.Row = 0;
            m_SetEllipse.Col = 0;
            m_SetEllipse.Phi = 0;
            m_SetEllipse.Raduise1 = 0;
            m_SetEllipse.Raduise2 = 0;
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
            if (m_SetEllipse != null)
            {
                m_SetEllipse.ReadParameter();
                Combox_LineWidth.Text = m_SetEllipse.LineWidth.ToString();
                Combox_CrossSize.Text = m_SetEllipse.CrossSize.ToString();
                Combox_CrossColor.Text = m_SetEllipse.CrossColor.ToString();
                Combox_CalibColor.Text = m_SetEllipse.CalibColor.ToString();
                Combox_LinesColor.Text = m_SetEllipse.EllipseColor.ToString();
                this.propertyGrid1.SelectedObject = m_SetEllipse;
                this.propertyGrid1.Refresh();
                RefreshContour();
                tool_ResultInfo.Text = "刷新成功";
            }
        }

        private void btn_SaveParameter_Click(object sender, EventArgs e)
        {
            try
            {

                m_SetEllipse.LineWidth = Combox_LineWidth.Text.ToInt32();
                m_SetEllipse.CrossSize = Combox_CrossSize.Text.ToInt32();
                m_SetEllipse.CrossColor = Combox_CrossColor.Text;
                m_SetEllipse.CalibColor = Combox_CalibColor.Text;
                m_SetEllipse.EllipseColor = Combox_LinesColor.Text;
                m_SetEllipse.WriteParameter();
                MessageBox.Show("参数保存成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void checkBo_All_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBo_All.Checked)
            {
                checkBox_Location.Checked = true;
                checkBox_Lines.Checked = true;
                checkBox_Caliper.Checked = true;
                m_SetEllipse.DisplayLoction = true;
                m_SetEllipse.DisplayEllipse = true;
                m_SetEllipse.DisplayCalib = true;
            }
        }

        private void checkBox_Location_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_Location.Checked)
            {
                m_SetEllipse.DisplayLoction = true;
            }
            else
            {
                m_SetEllipse.DisplayLoction = false;
                if (checkBo_All.Checked)
                {
                    checkBo_All.Checked = false;
                }
            }
        }

        private void checkBox_Caliper_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_Caliper.Checked)
            {
                m_SetEllipse.DisplayCalib = true;
            }
            else
            {
                m_SetEllipse.DisplayCalib = false;
                if (checkBo_All.Checked)
                {
                    checkBo_All.Checked = false;
                }
            }
        }

        private void checkBox_Lines_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_Lines.Checked)
            {
                m_SetEllipse.DisplayEllipse = true;
            }
            else
            {
                m_SetEllipse.DisplayEllipse = false;
                if (checkBo_All.Checked)
                {
                    checkBo_All.Checked = false;
                }
            }
        }

        private void hWindowControl1_HMouseWheel(object sender, HMouseEventArgs e)
        {
            MyHOperatorSet.ZoomImage(hWindowControl1, m_Image, e);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            HOperatorSet.CountObj(m_Image, out HTuple tuple1);
            if (tuple1.I == 0)
            {
                tool_ResultInfo.Text = "图像为空，请加载图像！";
                MessageBox.Show("图像为空，请加载图像！");
                return;
            }


            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
            m_SetEllipse.LineWidth = Combox_LineWidth.Text.ToInt32();
            m_SetEllipse.CrossSize = Combox_CrossSize.Text.ToInt32();
            m_SetEllipse.CrossColor = Combox_CrossColor.Text;
            m_SetEllipse.CalibColor = Combox_CalibColor.Text;
            m_SetEllipse.EllipseColor = Combox_LinesColor.Text;
            if (m_SetEllipse.Row > 0 && m_SetEllipse.Col > 0)
            {
                m_SetEllipse.Fit_Ellipse(m_Image, hWindowControl1.HalconWindow, m_SetEllipse.Row, m_SetEllipse.Col, m_SetEllipse.Phi, m_SetEllipse.Raduise1, m_SetEllipse.Raduise2);
                if (m_SetEllipse.EllipserOW.D > 0 && m_SetEllipse.FitRaduise1.D > 0)
                {
                    this.tool_ResultInfo.Text = $"拟合椭圆成功  ";
                }
            }

        }
    }
}
